<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>正则测试</title>
    <script src="./wasm_exec.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/xmdhs/searchqanda/style.css">
    <script>
        if (window.fetch == null || window.WebAssembly == null) {
            window.onload = function () {
                document.getElementById("ipInfo").innerHTML = "请更新浏览器，<a href=\"https://www.google.cn/intl/zh-CN/chrome/\">https://www.google.cn/intl/zh-CN/chrome/</a>";
            }
        }
    </script>
    <script>
        let go;
        try {
            go = new Go();
        } catch (error) {
            window.onload = function () {
                document.getElementById("ipInfo").innerHTML = "请更新浏览器，<a href=\"https://www.google.cn/intl/zh-CN/chrome/\">https://www.google.cn/intl/zh-CN/chrome/</a>";
            }
        }
        async function fetchAndInstantiate() {
            const response = await fetch("./ip.wasm");
            const buffer = await response.arrayBuffer();
            const obj = await WebAssembly.instantiate(buffer, go.importObject)
            go.run(obj.instance);
        }
        fetchAndInstantiate();
        function Form() {
            let text = document.querySelector("#confirmationText").value;
            if (text == "") {
                return;
            }
            let r = document.querySelector("#reg").value;
            let t = document.querySelector("#vehicle").checked;
            let l = reg(r, text, t);
            let f = document.getElementById("text");
            if (typeof (l.err) != "undefined") {
                text = l.err;
                f.innerText = text;
                return
            }
            let div = document.createElement("div")

            if (t) {
                for (const a of l) {
                    div.appendChild(newTable(a))
                }
            } else {
                div.appendChild(newTable(l))
            }

            f.innerHTML = div.innerHTML;
            return false;
        }
        window.addEventListener("load", () => {
            let f = document.getElementById("info");
            f.addEventListener("submit", function (event) {
                event.preventDefault();
                Form();
            });
        })
        function newTable(list) {
            let table = document.createElement("table")
            let tbody = document.createElement("tbody")
            for (const t of list) {
                tbody.appendChild(newtr(t))
            }
            table.appendChild(tbody)
            return table
        }

        function newtr(v) {
            let tr = document.createElement("tr")
            let td = document.createElement("td")
            td.innerText = v
            tr.appendChild(td)
            return tr
        }
    </script>
</head>

<body>
    <div class="container-lg px-3 my-5 markdown-body">
        <form id="info">
            <textarea id="confirmationText" autocomplete=on class="text" cols="86" rows="20" name="confirmationText"
                style="width: 100%;overflow: auto;word-break: break-all;"></textarea>
            <input type="search" id="reg">
            <input type="checkbox" id="vehicle" value="all">all<br>
            <input type="submit" value="look" class="submitButton">
        </form>
        <div id="text"></div>
    </div>
</body>

</html>